package com.bjxst;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
/**将对象序列化到文件*/
/*Users是实体类*/
//出现的是乱码，因为是以字节形式输出
public class ObjectOutputStreamObjectTypeDemo {
    public static void main(String[] args) {
        ObjectOutputStream oos = null;
        try{
            //必须用字节输出流对象 序列化与反序列化是基于字节
            oos = new ObjectOutputStream(new FileOutputStream("d:/x3.txt"));
            Users users = new Users(1,"hua","18");
            oos.writeObject(users);//将内容写到外部，叫对象序列化
            oos.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(oos != null){
                    oos.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
